<?php
namespace app\admin\controller;

use \think\Controller as Controller;
use \app\admin\model\Article as ArtModel;
use \think\Paginator;
use \think\Request as Request;
class Article extends Base{
    public function index(){
        $page = Request::instance()->get('page');
        $page = isset($page) ? ($page-1)*10 : 0;
        $model = new ArtModel;
        $data = $model->limit($page,10)->order('art_id desc')->select();
        $page = $model->paginate(10);
        $this->assign('data',$data);
        $this->assign('page',$page);

        //类别序列化
        $cat_model = new \app\admin\model\Category;
        $cat_data = $cat_model->field('cat_id,cat_name')->select();
        $cat = [];
        foreach($cat_data as $key=>$value){
            $cat[$value['cat_id']] = $value['cat_name'];
            
        }

        $this->assign('cat',$cat);
        return $this->fetch();
    }
    public function listDoc(){
        
    }

    //添加的
    public function add(){
        $cat_model = new \app\admin\model\Category;
        $cat_data = $cat_model->field(['cat_id','cat_name'])->select();
        $this->assign('cat_data',$cat_data);
        return $this->fetch();
    }

    public function insert(Request $req){
        $data = $req->post();
        
        if(strlen($data['title'] > 60)){
            $this->error('你输入的标题太长了');
        }
        if(strlen($data['nick_title'] > 60)){
            $this->error('副标题太长');
        }
        if(strlen($data['author'] > 60)){
            $this->error('名字太长');
        }
        $all_data = [
            'art_cat_id' => $data['cat_id'],
            'art_title' => $data['title'],
            'art_nicktitle' => $data['nick_title'],
            'art_author' => $data['author'],
            'art_type' => $data['art_type'],
            'art_source' => $data['article_source'],
            'art_ischeck' => $data['is_check'],
            'art_order' => $data['art_order'],
            'art_content' => $data['content'],
            'art_create_time' => $data['date'],

        ];
        $model = new ArtModel;
        
        if($model->save($all_data)){
            $this->success('文章插入成功');
        }else{
            $this->error('文章插入失败');
        }
        
    }
    //编辑的
    public function edit(){
        $article_id = Request::instance()->get('id','int');
        if(isset($article_id)){
            $model = new ArtModel;
            $data = $model->where(['art_id' => $article_id])->find();
            if(empty($data)){
                $this->error('内容不存在');
            }else{
                $cat_model = new \app\admin\model\Category;
                $cat_data = $cat_model->field(['cat_id','cat_name'])->select();
                $this->assign('cat_data',$cat_data);        
                $this->assign('data',$data);
                return $this->fetch();
            }            
        }else{
            $this->error('访问出错');
        }
    }

    public function update(){
        $data = $this->req->post();
        $id = $data['id'];
        if(isset($id)){
            if(strlen($data['title'] > 60)){
                $this->error('你输入的标题太长了');
            }
            if(strlen($data['nick_title'] > 60)){
                $this->error('副标题太长');
            }
            if(strlen($data['author'] > 60)){
                $this->error('名字太长');
            }
            $all_data = [
                'art_id'          => $id,
                'art_cat_id'     => $data['cat_id'],
                'art_title'       => $data['title'],
                'art_nicktitle'   => $data['nick_title'],
                'art_author'      => $data['author'],
                'art_type'        => $data['art_type'],
                'art_source'      => $data['article_source'],
                'art_ischeck'     => $data['is_check'],
                'art_order'       => $data['art_order'],
                'art_content'     => $_POST['content'],
                'art_create_time' => $data['date'],
                'art_up_time'     => time(),
            ];
            // var_dump($all_data);
            $model = new ArtModel;
        
            if($model->update($all_data)){
                $this->success('文章更新成功');
            }else{
                $this->error('文章更新失败');
            }
            
        }
    }
    //删除的
    public function remove(){
        
    }

    //动态刷新的
    public function getAjax(){
        
    }

    public function ajaxArt(){
        $page = Request::instance()->get('id');
        $page = isset($page) ? ($page-1)*10 : 0;
        $model = new ArtModel;
        $data = $model->limit($page,10)->order('art_id desc')->select();
        $this->assign('data',$data);
        echo $this->fetch('table');
    }

    public function upimage(){
        $file = $this->req->file('image');
        if($file){
            // $path = ROOT_PATH.'/files/images';
            // echo $path;
            $info = $file->move(ROOT_PATH . 'files/images/');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                // echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                echo json_encode(['errno'=>0,'data'=>['/files/images/'.$info->getSaveName()]]);
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                // echo $info->getFilename(); 
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
    }
}
